package institution_service

import (
	"github.com/EDDYCJY/go-gin-example/models"
	"github.com/shopspring/decimal"
)

type GetBill struct {
	InstitutionID int
	PageNum       int
	PageSize      int
}

func (t *GetBill) GetBill() (interface{}, error) {
	maps := map[string]interface{}{
		"institution_id": t.InstitutionID,
	}
	total, _ := models.GetBillTotal(maps)
	goodsList, _ := models.GetBillPage([]string{"bill_id", "streamer_id", "out_order_sn", "express_no", "express_name", "weight", "freight",
		"institution_id", "goods_id", "selling_price", "supply_price"}, maps, t.PageNum, t.PageSize, "create_time desc")
	var lists []interface{}
	for _, v := range goodsList {
		sellingPrice := decimal.NewFromInt(int64(v.SellingPrice)).Div(decimal.NewFromInt(100))
		supplyPrice := decimal.NewFromInt(int64(v.SupplyPrice)).Div(decimal.NewFromInt(100))
		params := map[string]interface{}{
			"bill_id":         v.BillID,
			"goods_id":        v.GoodsID,
			"goods_name":      v.Goods.GoodsName,
			"goods_spec":      v.Goods.GoodsSpec,
			"goods_no":        v.Goods.GoodsNo,
			"selling_price":   sellingPrice,
			"supply_price":    supplyPrice,
			"create_time":     v.CreateTime.Format("2006-01-02 15:04:05"),
			"goods_img":       v.Goods.GoodsImg,
			"goods_num":       v.Goods.GoodsNum,
			"streamer_name":   v.Streamer.StreamerName,
			"streamer_mobile": v.Streamer.StreamerMobile,
			"out_order_sn":    v.OutOrderSn,
			"express_no":      v.ExpressNo,
			"express_name":    v.ExpressName,
			"weight":          v.Weight,
			"freight":         v.Freight,
		}
		lists = append(lists, params)
	}
	return map[string]interface{}{
		"lists": lists,
		"total": total,
	}, nil
}
